package com.fare.taluo.core.domain.crawl.adapter.sub;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Date;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.util.EntityUtils;

import com.fare.taluo.conf.ConfigContainer;
import com.fare.taluo.core.domain.crawl.CrawlTaskModel;
import com.fare.taluo.core.domain.crawl.adapter.AbstractAdapter;
import com.fare.taluo.util.DateUtils;

public class HNAirLinePdfAdaptor extends AbstractAdapter {
	private HttpClient client = null;
	public HNAirLinePdfAdaptor(CrawlTaskModel taskModel) {
		super(taskModel);
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Object> paraseToVo(Object fetchObject) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public String getUrl() throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public Object fetch(String url) throws Exception {
		return this.fetchURI(url);
	}
	public File fetchURI(String uri) throws Exception {
		 URL url = new URL(uri);
         HttpURLConnection  httpConn = (HttpURLConnection) url.openConnection();
         HttpURLConnection.setFollowRedirects(true);
         InputStream in = httpConn.getInputStream();
         if(null != in){
        	 return writeFile(in);
         }else{
        	 return null;
         }
        
	}
	
	/**
	 * 保存pdf
	 * @param is
	 * @return
	 * @throws IOException
	 */
	protected File writeFile(InputStream is) throws IOException {
		FileOutputStream fos = null;
		BufferedInputStream bis = null;
		int BUFFER_SIZE = 1024;
		byte[] buf = new byte[BUFFER_SIZE];
		int size = 0;
		bis = new BufferedInputStream(is); 
        //建立文件 
		File file = new File(DateUtils.dateToStr(new Date(),
				"yyyy-MM-dd")+"-HN.pdf");
	    fos = new FileOutputStream(file);   
	    try{
	    //保存文件   
	    while ( (size = bis.read(buf)) != -1)    
	    { fos.write(buf, 0, size); }  
	    }finally{
	    	if(fos != null) fos.close();   
	    	if(bis != null)  bis.close();
	    	fos = null;
	    	bis = null;
	    }
	   
		return file;
	}
	@Override
	public boolean validateFetch(Object fetchObject) throws Exception {
		// TODO Auto-generated method stub
		return false;
	}
    public static void main(String[] args) {
    	try {
			ConfigContainer.getInstance().register("com.fare.taluo.core.domain.crawl.CrawlPropertiesLoader");
			
			new HNAirLinePdfAdaptor(null).fetchURI("http://www.hnair.com/lxxx/hbskb/201005/P020130603327385934157.pdf");
		} catch (Exception e) {                 //  http://221.176.14.91/www.hnair.com/lxxx/hbskb/201005/P020130603327385934157.pdf
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
